package ar.com.ford.it.fullEconomics.persistence.dao;

import java.util.Collection;
import java.util.LinkedList;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr01Request;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr02Requestitm;
import ar.com.ford.it.fullEconomics.struct.RequestFindStruct;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class RequestDao extends Dao {
    private static final String CLASS_NAME = CommodityDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Crea el request pasado por parametro
     * 
     * @param request
     * @throws PersistenceException
     */
    public void createRequest(Teesr01Request request)
            throws PersistenceException {

        String METHOD_NAME = "createRequest";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(request);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Busca todos los request diltrando pro los parametros pasados en el findStruct
     * 
     * @param findStruct
     * @return
     * @throws PersistenceException
     */
    public Collection<Teesr01Request> findRequests(RequestFindStruct findStruct)
            throws PersistenceException {

        String METHOD_NAME = "findRequests";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesr02Requestitm> requestItemColl = null;
        Collection<Teesr01Request> resultCol = new LinkedList<Teesr01Request>();
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query;
        if (findStruct.getCommodity() != -1
                || !findStruct.getPartClass().equalsIgnoreCase("-1")
                || !StringUtils.isBlank(findStruct.getPrefix())
                || !StringUtils.isBlank(findStruct.getBase())
                || !StringUtils.isBlank(findStruct.getSuffix())) {
            query = pm.newQuery("findRequestItems", Teesr02Requestitm.class);
            query.setParameters(findStruct);
            requestItemColl = (Collection) pm.executeQuery(query);
            for (Teesr02Requestitm item : requestItemColl) {
                if (!resultCol.contains(item.getTeesr01Request()))
                    resultCol.add(item.getTeesr01Request());
            }
        } else {
            query = pm.newQuery("findRequests", Teesr01Request.class);
            query.setParameters(findStruct);
            resultCol = (Collection) pm.executeQuery(query);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }

    /**
     * Devuelve un request para ser modificado por el logic
     * 
     * @param code
     * @param plant
     * @return
     * @throws PersistenceException
     */
    public Teesr01Request getRequestForUpdate(Integer code, Teesg01Plant plant)
            throws PersistenceException {

        String METHOD_NAME = "getRequestForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesr01Request result = null;
        Teesr01Request pk = new Teesr01Request();

        pk.setEesr01RequestNumberK(code);
        pk.setEesr01PlantCodeC(plant);

        result = (Teesr01Request) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Borra el price increase request pasado por parametro
     * 
     * @param req
     * @throws PersistenceException
     */
    public void deleteRequest(Teesr01Request req)
            throws PersistenceException {

        String METHOD_NAME = "deleteRequest";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        if (tx != null) {
            tx.registerDelete(req);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
    }
}
